import prompt from '@system.prompt';
import router from '@system.router';
import http from '@ohos.net.http';
import {expandsList, incomesList} from '../../common/iconInfo.js';

//页面逻辑再理一遍

export default {
    //初始化,计算当月以及当天收支,显示账单数据(星期、金额、备注、分类图标)
    onInit() {
        this.culcMoney();
    },
    data: {
        newDate: JSON.stringify(new Date()).substring(1, 8),
        todayDate: JSON.stringify(new Date()).substring(1, 8),
        weekList: ["日", "一", "二", "三", "四", "五", "六"],
        monthIncomes: 0,
        monthExpands: 0,
        week: 1,
        userId: 1,
        "monthDetailList": [
            {
                "todayDetailList": [
                    {
                        "id": 6,
                        "money": "-2",
                        "sort": "1",
                        "note": "这是备注"
                    },
                    {
                        "id": 7,
                        "money": "222",
                        "sort": "2",
                        "note": null
                    },
                    {
                        "id": 10,
                        "money": "-23",
                        "sort": "3",
                        "note": "这是备注"
                    }, {
                        "id": 11,
                        "money": "2.3",
                        "sort": "4",
                        "note": "这是备注"
                    },
                    {
                        "id": 12,
                        "money": "1.22",
                        "sort": "4",
                        "note": "这是备注"
                    }, {
                        "id": 13,
                        "money": "9.9",
                        "sort": "5",
                        "note": "这是备注"
                    },
                    {
                        "id": 14,
                        "money": "9.9",
                        "sort": "7",
                        "note": "这是备注"
                    }, {
                        "id": 15,
                        "money": "-9.95",
                        "sort": "11",
                        "note": "这是备注"
                    }],
                "date": "2022-04-01"
            },
            {
                "todayDetailList": [
                    {
                        "id": 26,
                        "money": "-12.2",
                        "sort": "10",
                        "note": "这是一条备注啦"
                    },
                    {
                        "id": 27,
                        "money": "-12.9",
                        "sort": "4",
                        "note": "这是一条备注啦"
                    },
                    {
                        "id": 28,
                        "money": "12.4", 
                        "sort": "6",
                        "note": "这是一条备注啦"
                    },
                    {
                        "id": 29,
                        "money": "-12.2",
                        "sort": "6",
                        "note": "这是一条备注啦"
                    },
                    {
                        "id": 32,
                        "money": "12.2",
                        "sort": "3",
                        "note": "这是一条备注啦"
                    },
                    {
                        "id": 34,
                        "money": "12.2",
                        "sort": "6",
                        "note": "这是一条备注啦"
                    },
                    {
                        "id": 36, 
                        "money": "-12.2",
                        "sort": "6",
                        "note": "这是一条备注啦"
                    },
                    {
                        "id": 37,
                        "money": "-66",
                        "sort": "6",
                        "note": null
                    }
                ],
                "date": "2022-04-02"
            },
        ],
        sideBarList: [
            {
                title: "搜索"
            },
            {
                title: "统计"
            },
            {
                title: "多张本管理"
            },
            {
                title: "钱包管理"
            },
            {
                title: "周期记账"
            },
            {
                title: "分类管理"
            },
            {
                title: "预算管理"
            },
            {
                title: "回收站"
            },
            {
                title: "提醒设置"
            },
            {
                title: "自定义背景"
            },
            {
                title: "添加到手机桌面"
            },

        ]
    },
    culcMoney(){
        let todayIncomes=0,todayExpands=0,billMoney=0,sortIndex=0;
        let monthIncomes=0,monthExpands=0;
        this.monthDetailList.forEach(item => { //得到当月 每天 的数据
            todayIncomes=0;todayExpands=0;billMoney=0;sortIndex=0;
            let date = item.date;
            item.week = this.weekList[(new Date(date)).getDay()];//当天星期
            item.todayDetailList.forEach(bill => {//得到当天的 每条 数据
                bill.date = date;
                sortIndex = Number(bill.sort)  //分类index ->  根据分类id确定账单分类，获取imgInfo对象
                billMoney = Number(bill.money) //单条账单money
                if(billMoney > 0){  //收入金额为正,imgInfo对象所属分类数组为incomesList
                    todayIncomes = todayIncomes + billMoney;
                    bill.imgInfo = incomesList[sortIndex]
                }else{   //支出金额为负,所属分类数组为expandsList
                    todayExpands = todayExpands + billMoney;
                    bill.imgInfo = expandsList[sortIndex]
                }
            });
            item.todayIncomes = todayIncomes.toFixed(2)//当天总收入
            item.todayExpands = todayExpands.toFixed(2)//当天总支出
            console.log('todayIncomes:'+item.todayIncomes)
            console.log('todayExpands:'+item.todayExpands)
            monthIncomes = monthIncomes + todayIncomes;
            monthExpands = monthExpands + todayExpands;
        });
        this.monthIncomes = monthIncomes.toFixed(2)  //当月总收入
        this.monthExpands = monthExpands.toFixed(2)  //当月总支出
    },
//    点击时间选择器，选择查看某月的账单
    itemClick(e) {
//        console.log( 'itemClick'+ JSON.stringify(e))
        this.newDate = e.detail.time;
        let httpRequest = http.createHttp();
        httpRequest.request(
            `http://120.78.198.170:3300/queryBill?userId=${this.userId}&month=${this.newDate}`,
            {
                method: 'GET',
                readTimeout: 60000,
                connectTimeout: 600
            }, (err, data) => {
            if (!err) {
                this.monthDetailList = data.result.monthDetailList
                console.info('Result:' + data.result);
                console.info('code:' + data.responseCode);
            } else {
                console.info('error:' + err.data);
            }
        }
        )
    },
    billClick(bill){},
    //实现单击修改账单
    changeDetail(bill){
        console.log(JSON.stringify(bill))
        router.push({
            uri: "pages/addBill/addBill",
            params:({
                newDate:bill.date,
                amount:Number(bill.money),
                note:bill.note,
                sort:Number(bill.sort),
                billId:Number(bill.id),
            })
        })
    },
    //实现长按删除
    longPressDelete(bill,e){
        console.log(JSON.stringify(e))
        prompt.showDialog({
            message: '是否确定删除账单?',
            buttons: [
                {
                    text: '取消',
                    color: ''
                },
                {
                    text: '确认',
                    color:''
                }
            ],
            success: (data) =>{
                if(data.index == 1){//确认删除
                    let idx = e.target.attr.idx//获取索引
                    this.monthDetailList.forEach(item => {//在渲染数组中删除bill
                        if (item.todayDetailList[idx].id === bill.id){
                            item.todayDetailList.splice(idx,1)
                        }
                    });
                    //重新计算money
                    this.culcMoney();
                    this.onInit();
                    //发送请求，删除账单
                    let httpRequest = http.createHttp();
                    httpRequest.request(
                        `http://120.78.198.170:3300/deleteBill?billId=${bill.id}`,
                        {
                            method: 'GET',
                            readTimeout: 60000,
                            connectTimeout: 600
                        }, (err, data) => {
                        if (!err) {//请求成功
                            console.info('Result:' + data.result);
                            console.info('code:' + data.responseCode);

                        } else {
                            console.info('error:' + err.data);
                        }
                    }
                    )
                }
            },
            cancel: function () {
                console.log('handling cancel')
            },
            fail: function (data, code) {
                console.log(`handling fail, code = ${code}`)
            }
        })

    },
    clickToCurrent(){
        console.log(this.todayDate);
        this.newDate = this.todayDate
        console.log(this.newDate);
    },
    clickToAddPage() {
        router.push({
            uri: "pages/addBill/addBill"
        })
    },
}